package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimall.product.dao.AttrDao;
import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.WithAttrVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrDao attrDao;
    @Autowired
    private AttrAttrgroupRelationDao attrgroupRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        //如果三级分类位空的时候，检索所有
        if (catelogId == 0) {
            IPage<AttrGroupEntity> groupEntityIPage = this.page(new Query<AttrGroupEntity>().getPage(params), new QueryWrapper<AttrGroupEntity>());
            return new PageUtils(groupEntityIPage);
        } else {
            //通过三级id获取属性分组
            QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>().eq("attr_group_id", catelogId);
            if (StringUtils.isNotEmpty((String) params.get("key"))) {
                String key = String.valueOf(params.get("key"));
                queryWrapper.and((obj) -> {
                    obj.eq("attr_group_id", key).or().like("attr_group_name", key);
                });
            }
            IPage<AttrGroupEntity> groupEntityIPage = this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
            return new PageUtils(groupEntityIPage);
        }
    }

    @Override
    public List<WithAttrVo> withAttrList(Long catelogId) {
        //查询三级分类下的的所有分组信息
        List<AttrGroupEntity> groupEntityList = this.baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        //一个分组id会有多个属性信息
        List<WithAttrVo> withAttrVoList = groupEntityList.stream().map((item) -> {
            WithAttrVo withAttrVo = new WithAttrVo();
            //1.先查出分组id在 属性&属性分组关联 的信息
            List<AttrEntity> attrEntityList = new ArrayList<>();
            if (item.getAttrGroupId()!=null){
                List<AttrAttrgroupRelationEntity> relationEntities = attrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", item.getAttrGroupId()));
                List<Long> longList = relationEntities.stream().map(relationEntity -> relationEntity.getAttrId()).collect(Collectors.toList());

                //2.根据得出的attrId，查处属性信息，分组跟属性是一对多的关系
                if (!longList.isEmpty()){
                    attrEntityList = attrDao.selectBatchIds(longList);
                }
            }
            BeanUtils.copyProperties(item, withAttrVo);
            withAttrVo.setAttrs(attrEntityList);
            return withAttrVo;
        }).collect(Collectors.toList());

        return withAttrVoList;
    }
}